﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Media.Animation;

namespace WPF.Controls.QuerySelector
{
    public static class Effect
    {
        public static FrameworkElement show(this FrameworkElement element)
        {
            element.Opacity = 0;
            element.Visibility = Visibility.Visible;
            element.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
            {
                double height = element.ActualHeight > 0 ? element.ActualHeight : element.Height;
                double width = element.ActualWidth > 0 ? element.ActualWidth : element.Width;
                
                // Animate the Button's Width.
                DoubleAnimation myDoubleAnimation = new DoubleAnimation();
                myDoubleAnimation.From = 0;
                myDoubleAnimation.To = 300;
                myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.3));
                myDoubleAnimation.AutoReverse = false;
                Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(FrameworkElement.WidthProperty));

                // Animate the Button's Width.
                DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
                myDoubleAnimation1.From = 0;
                myDoubleAnimation1.To = height;
                myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.3));
                myDoubleAnimation.AutoReverse = false;
                Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath(FrameworkElement.HeightProperty));

                DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
                myDoubleAnimation2.From = 0;
                myDoubleAnimation2.To = 1;
                myDoubleAnimation2.Duration = new Duration(TimeSpan.FromSeconds(0.3));
                myDoubleAnimation2.AutoReverse = false;
                Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath(FrameworkElement.OpacityProperty));

                Storyboard myWidthAnimatedButtonStoryboard = new Storyboard();
                myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation);
                myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation1);
                myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation2);
                element.BeginStoryboard(myWidthAnimatedButtonStoryboard);
                myWidthAnimatedButtonStoryboard.Begin(element);
                // Apply the animation to the button's Width property.
                
            }));
                
            return element;
        }

        /// <summary>
        ///  ("slow", "normal", or "fast") or the number 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="speed"></param>
        /// <returns></returns>
        public static FrameworkElement show(this FrameworkElement element,object speed)
        {
            element.Visibility = Visibility.Visible;
            return element;
        }

        public static FrameworkElement hide(this FrameworkElement element)
        {
            element.Visibility = Visibility.Collapsed;
            return element;
        }
    }
}
